import pygame, sys, math

class Glass():
    def __init__(self, speed = [2,2], size = [100,100], pos = (0,0)):
        """
        self.images = []
        for image in images:
            newimage = pygame.image.load(image)
            newimage = pygame.transform.scale(newimage, size)
            self.images += [newimage]
        self.frame = 50
        self.maxFrame = len(self.images)-1
        self.waitCount = 0
        self.waitMax = 10
        self.image = self.images[self.frame]
        """
        self.image = pygame.image.load("Resources/Glass/glass.png")
        self.size = size
        self.enlargedSize = [size[0]*3, size[1]]
        self.image = pygame.transform.scale(self.image, self.size)
        self.rect = self.image.get_rect()
        self.maxSpeedx = speed[0]
        self.maxSpeedy = speed[1]
        self.speedx = 0
        self.speedy = 0
        self.speed = [self.speedx, self.speedy]
        self.radius = self.rect.width/2
        self.place(pos)
        self.bigTimerMax = 60*10
        self.bigTimer = 0
        
    def place(self, pos):
        self.rect.center = pos
        
    def direction(self, dir):
        if dir == "right":
            self.speedx = self.maxSpeedx
        if dir == "stop right":
            self.speedx = 0
        if dir == "left":
            self.speedx = -self.maxSpeedx
        if dir == "stop left":
            self.speedx = 0
        if dir == "up":
            self.speedy = -self.maxSpeedy
        if dir == "stop up":
            self.speedy = 0
        if dir == "down":
            self.speedy = self.maxSpeedy
        if dir == "stop down":
            self.speedy = 0
    
    def update(self):
        self.move()
        #self.animate()
        if self.bigTimer > 1:
            self.bigTimer -= 1
        elif self.bigTimer == 1:
            self.bigTimer = 0
            self.image = pygame.transform.scale(self.image, self.size)
            self.rect = self.image.get_rect(center = self.rect.center)

    
    def animate(self):
        if self.waitCount < self.waitMax:
            self.waitCount += 1
        else:
            self.waitCount = 0
            if self.frame < self.maxFrame:
                self.frame += 1
            else:
                self.frame = 0
            self.image = self.images[self.frame]
    
    def move(self):
        self.speed = [self.speedx, self.speedy]
        self.rect = self.rect.move(self.speed)
        
    def collideWall(self, width, height):
        if self.rect.left < -self.rect.width:
            self.rect.center = [width, self.rect.center[1]]
        elif self.rect.right > width +self.rect.width:
            self.rect.center = [0, self.rect.center[1]]
            
    def collideBall(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    other.living = False
                    return True
        return False
                     
    def collidePowerup(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    other.living = False
                    if other.type == "Enlarge":
                        self.image = pygame.transform.scale(self.image, self.enlargedSize)
                        self.bigTimer = self.bigTimerMax
                        self.rect = self.image.get_rect(center = self.rect.center)
        
    
    def distanceToPoint(self, pt):
        x1 = self.rect.center[0]
        y1 = self.rect.center[1]
        x2 = pt[0]
        y2 = pt[1]
        
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
    
    
    
    
    